module game{
    export class GiftView extends BaseEuiView{

        public btn_close:eui.Image;
        public btn_share:eui.Image;

        public txt_title:eui.Label;

        public reset:eui.Image;
        public hammar:eui.Image;
        public constructor($m:BaseController,$parent:eui.Group){
            super($m,$parent);
            this.skinName = "resource/skin/Gift.exml";
        }

        /**
         *对面板进行显示初始化，用于子类继承
            *
            */
        public initUI():void{
            super.initUI();

            this.btn_close.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onClickHandler,this);
            this.btn_share.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onClickHandler,this);
            this.skin.width = egret.MainContext.instance.stage.stageWidth;
            this.skin.height = egret.MainContext.instance.stage.stageHeight;
        }

        /**
         * 当前大礼包类型
         * 0：悔棋
         * 1：锤子
         */
        private itemType:number
        public open(...param:any[]):void {
            let random = Math.random();
            if(random <= 0.7){
                this.reset.visible = true;
                this.hammar.visible = false;
                this.itemType = 0;
                this.txt_title.text = "获得撤销大礼包";
            }else{
                this.reset.visible = false;
                this.hammar.visible = true;
                this.itemType = 1;
                this.txt_title.text = "获得锤子大礼包";
            }

            this.showEffect();
           
        }

        
        private showEffect(){
            let target = this.itemType ? this.hammar :this.reset;
            let ox = target.x;

            let count = 0;
            let gaptime = 100;
            let tween_left = function(){
                count++;
                if(count * gaptime == 1000){
                    egret.Tween.removeTweens(target);
                    target.x = ox;
                    return;
                }
                egret.Tween.removeTweens(target);
                egret.Tween.get(target).to({x:ox-4},gaptime).call(tween_right,this);
            }

            let tween_right = function(){
                count++;
                if(count * gaptime == 1000){
                    egret.Tween.removeTweens(target);
                    target.x = ox;
                    return;
                }
                egret.Tween.removeTweens(target);
                egret.Tween.get(target).to({x:ox + 4},gaptime).call(tween_left,this);
            }

            tween_left();
        }

        private onClickHandler(e:egret.TouchEvent){
            switch(e.target){
                case this.btn_close:
                    //合规
                    if(this.itemType == 0)gameData.addResetItem();
                    else gameData.addHammarItem();
                    App.ViewManager.close(ViewConst.Gift);
                    
                    this.targetSuccess();
                break;
                case this.btn_share:
                break;
            }
        }


        private targetSuccess(){
             //大于15个弹提示
                if (gameData.curmax >= gameData.targetNumber) {
                    App.ViewManager.open(ViewConst.GameGoal);
                }
        }
    }
}